package cn.anc.aonicardv.module.adpter.recorder;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.anc.aonicardv.GlideApp;
import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.AlbumBean;
import cn.anc.aonicardv.bean.VideoBean;
import cn.anc.aonicardv.event.AllSelectEvent;
import cn.anc.aonicardv.event.EditEvent;
import cn.anc.aonicardv.event.SelectEvent;
import cn.anc.aonicardv.module.adpter.listener.OnAlbumItemClickListener;
import cn.anc.aonicardv.module.adpter.listener.OnAlbumItemLongClickListener;
import cn.anc.aonicardv.module.ui.recorder.RecorderVideoFragment;
import cn.anc.aonicardv.util.DeviceInfo;
import cn.anc.aonicardv.util.LogUtil;
import cn.anc.aonicardv.util.StatusBarUtil;
import cn.anc.aonicardv.util.UIUtil;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.cache.CacheManager;

/**
 * Created by yangdai on 2017/5/19.
 */

public class RecorderVideoAdapter extends RecyclerView.Adapter {

    private List<AlbumBean> videoBeanList = new ArrayList<>();
    private boolean isEditMode;
    private RecorderVideoFragment mFragment;
    private int mRecorderItemHeight;


    public RecorderVideoAdapter(RecorderVideoFragment context) {
        this.mFragment = context;
        int ScreenHeight =  DeviceInfo.ScreenHeightPixels;
        int statusBarHeight = StatusBarUtil.getStatusBarHeight(context.getContext());
        //标题栏加标签栏加间隔加留白
        int otherHeight =  UIUtil.dip2px(150);
        mRecorderItemHeight = (ScreenHeight-statusBarHeight -otherHeight )/2;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file_date, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        layoutParams.height = mRecorderItemHeight;
        viewHolder.fileDateTv.setText(videoBeanList.get(position).getDate()+"");

//        viewHolder.selectCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (viewHolder.selectCb.isPressed()) {
//                    videoBeanList.get(position).setCheck(isChecked);
//                    EventBus.getDefault().post(new SelectEvent(isChecked, videoBeanList.size()));
//                }
//            }
//        });
        if (videoBeanList.get(position).isCheck()) {
            viewHolder.selectCb.setChecked(true);
            viewHolder.mask.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selectCb.setChecked(false);
            viewHolder.mask.setVisibility(View.GONE);
        }


        if (isEditMode) {
            viewHolder.selectCb.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selectCb.setChecked(false);
            viewHolder.selectCb.setVisibility(View.GONE);
        }

        viewHolder.mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEditMode) {
                    if (viewHolder.mask.getVisibility() == View.GONE)
                        viewHolder.mask.setVisibility(View.VISIBLE);
                    else
                        viewHolder.mask.setVisibility(View.GONE);
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(viewHolder.selectCb, position, isEditMode);
                }
            }
        });
        viewHolder.mRootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null)
                    onItemLongClickListener.onItemLongClick(v, position, isEditMode);
                return true;
            }
        });


    }

    /**
     * 刷新适配器数据列表
     *
     * @param dataList 待添加的数据列表
     */
    public void refreshDataList( List<AlbumBean> dataList) {
        if (dataList != null&&dataList.size()>0) {
            videoBeanList.clear();
            videoBeanList.addAll(dataList);
            LogUtil.e("llcTest0217", "refreshDataList:" + dataList.size());
            notifyDataSetChanged();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EditEvent event) {

        if (!mFragment.getUserVisibleHint())
            return;
        this.isEditMode = event.isEdit;
        for (AlbumBean p : videoBeanList) {
            p.setCheck(false);
        }
        notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AllSelectEvent event) {
        if (videoBeanList == null || !mFragment.getUserVisibleHint()) {
            return;
        }
        for (AlbumBean p : videoBeanList) {
            p.setCheck(event.isAllSelect);
        }
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return videoBeanList == null ? 0 : videoBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public RelativeLayout mRootView;
        public TextView  fileDateTv;
        public CheckBox selectCb;
        public View mask;

        public ViewHolder(View view) {
            super(view);
            mRootView = view.findViewById(R.id.rl_root_layout);
            fileDateTv = (TextView) view.findViewById(R.id.tv_file_date);
            selectCb = (CheckBox) view.findViewById(R.id.cb_select);
            mask = view.findViewById(R.id.mask);
        }
    }

    private OnAlbumItemClickListener onItemClickListener;

    private OnAlbumItemLongClickListener onItemLongClickListener;

    public void setOnItemClickListener(OnAlbumItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnAlbumItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void registerEventBusForAdapter(){
       EventBus.getDefault().register(this);
    }

    public void unregisterEventBusForAdapter(){
        EventBus.getDefault().unregister(this);
    }
}
